package com.indexingsystem.boss.utils;

import com.indexingsystem.boss.Vo.ExportThemeWordsResVo;
import com.indexingsystem.boss.Vo.RelationThemeWordsResVo;
import com.indexingsystem.boss.Vo.ThemeTableResVo;
import com.indexingsystem.boss.Vo.ThemeWordsResVo;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ProductIndexingXmlUtil {


    public void exportIndexing(ExportThemeWordsResVo vo, String filePath) throws FileNotFoundException, UnsupportedEncodingException {
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateFilePathStr = df2.format(new Date());
        String dateFileStr = df1.format(new Date());
        String exportPath = null;
        if (filePath.endsWith("\\")) {
            exportPath = filePath + dateFilePathStr;
        } else {
            exportPath = filePath + "\\" + dateFilePathStr;
        }
        File file = new File(exportPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        Document document = DocumentHelper.createDocument();

        Element root = document.addElement("root");
        Element products = root.addElement("products");
        products.addElement("productid").setText(vo.getProductId());
        products.addElement("name").setText(vo.getProductName() == null ? "" : vo.getProductName());
        products.addElement("exportTime").setText(df3.format(vo.getExportTime()));
        products.addElement("exportType").setText(vo.getExportType());
        Element tables = root.addElement("ThemeTables");

        List<ThemeTableResVo> voTables = vo.getTables();
        if (voTables != null && voTables.size() > 0) {
            for (ThemeTableResVo t : voTables) {
                addThemeTalbeElement(tables, t);
            }
        }

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        XMLWriter writer = new XMLWriter(new FileOutputStream(exportPath + "\\" + vo.getProductId() + dateFileStr + ".xml"), format);
        try {
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void addThemeTalbeElement(Element parent, ThemeTableResVo child) {
        Element element = parent.addElement("ThemeTable");
        element.addAttribute("id", String.valueOf(child.getThemeTableId()));
        element.addAttribute("name", child.getThemeTableName());
        element.addAttribute("code", child.getThemeTableCode());
        if (child.getThemeWords() != null && child.getThemeWords().size() > 0) {
            Element relationThemeWords = element.addElement("ThemeWords");
            for (ThemeWordsResVo t : child.getThemeWords()) {
                addThemeWordElement(relationThemeWords, t);
            }
        }
    }

    public void addThemeWordElement(Element parent, ThemeWordsResVo child) {
        Element element = parent.addElement("ThemeWord");
        element.addAttribute("id", String.valueOf(child.getThemeWordsId()));
        element.addAttribute("name", child.getThemeWordsName());
        element.addAttribute("code", child.getThemeWordsCode());
        element.addAttribute("extra_indexing", String.valueOf(child.getExtraIndexing()));
        if (child.getRelations() != null && child.getRelations().size() > 0) {
            Element relationThemeWords = element.addElement("RelationThemeWords");
            for (RelationThemeWordsResVo r : child.getRelations()) {
                addRelationThemeWordElement(relationThemeWords, r);
            }
        }
    }

    public void addRelationThemeWordElement(Element parent, RelationThemeWordsResVo child) {
        Element element = parent.addElement("RelationThemeWord");
        element.addAttribute("id", String.valueOf(child.getThemeWordsId()));
        element.addAttribute("name", child.getThemeWordsName());
        element.addAttribute("code", child.getThemeWordsCode());
    }

}
